﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QLNS.Models
{
    public class QuaTrinhCongTacModels
    {
        public static QTCongTac GetQTCongTac(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from s in context.DSQTCongTac
                         where s.ID == id
                         select s).FirstOrDefault();
            return query;

        }
        //danh sach dantoc
        public static List<QTCongTac> GetQTCongTacTheoCanBo(int id)
        {
            //khoi tao db
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from dt in context.DSQTCongTac
                        join ll in context.DSChucVu on dt.IDChucVu equals ll.ID
                        where dt.IDCanBo == id
                        orderby dt.TuNgay ascending
                        select dt;
            return query.ToList();
        }
        public static void Add(QTCongTac qhgd)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            context.DSQTCongTac.AddObject(qhgd);
            context.SaveChanges();
        }
        //sua dan toc
        public static void Update(QTCongTac id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSQTCongTac
                        where h.ID == id.ID
                        select h;
            QTCongTac kq = query.First<QTCongTac>();
            kq = id;
            context.DSQTCongTac.ApplyCurrentValues(kq);
            context.SaveChanges();

        }
        public static void Delete(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = (from h in context.DSQTCongTac
                         where h.ID == id
                         select h).FirstOrDefault();

            context.DSQTCongTac.DeleteObject(query);
            context.SaveChanges();

        }
        public static void DeleteAllOfPerson(int id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            var query = from h in context.DSQTCongTac
                        where h.IDCanBo == id
                        select h;
            foreach (var item in query)
            {
                context.DSQTCongTac.DeleteObject(item);
            }
            context.SaveChanges();
        }
    }
}